package com.laizhenghua.hotel.service.impl;

import com.laizhenghua.hotel.dao.UsersDao;
import com.laizhenghua.hotel.entity.UsersEntity;
import com.laizhenghua.hotel.utils.MainUtil;
import com.laizhenghua.hotel.utils.PageUtils;
import com.laizhenghua.hotel.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.laizhenghua.hotel.dao.ApplyDao;
import com.laizhenghua.hotel.entity.ApplyEntity;
import com.laizhenghua.hotel.service.ApplyService;

import javax.annotation.Resource;


@Service("applyService")
public class ApplyServiceImpl extends ServiceImpl<ApplyDao, ApplyEntity> implements ApplyService {
    @Resource
    private UsersDao usersDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ApplyEntity> page = this.page(
                new Query<ApplyEntity>().getPage(params),
                new QueryWrapper<ApplyEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ApplyEntity> getListByHouseId(int id) {
        List<ApplyEntity> applyEntityList = this.list(new QueryWrapper<ApplyEntity>().eq("hid", id));
        return applyEntityList;
    }

    @Override
    public void saveByUserNameAndTel(Map<String, Object> params, String username, String tel) {
        UsersEntity usersEntity = usersDao.selectOne(new QueryWrapper<UsersEntity>()
                .eq("username", username)
                .eq("tel", tel));
        ApplyEntity applyEntity = new ApplyEntity();
        applyEntity.setHid(Integer.parseInt(params.get("id").toString()));
        applyEntity.setUserid(usersEntity.getId());
        applyEntity.setState(1);
        applyEntity.setType(1);
        applyEntity.setCreated(MainUtil.getTime());
        applyEntity.setUsername(username);
        applyEntity.setTel(tel);
        applyEntity.setState(Integer.parseInt(params.get("sex").toString()));
        applyEntity.setIdcard(params.get("idcard").toString());
        applyEntity.setIndate(params.get("indate").toString());
        applyEntity.setOutdate(params.get("outdate").toString());
        applyEntity.setSex("1".equals(params.get("sex").toString()) ? "男" : "女");
        this.save(applyEntity);
    }

    @Override
    public ApplyEntity getOneByTelAndUsername(String tel, String username) {
        ApplyEntity applyEntity = this.getOne(new QueryWrapper<ApplyEntity>()
                .eq("tel", tel)
                .eq("username", username));
        return applyEntity;
    }
}